package com.fast.admin.common.utils;


import com.fast.admin.vo.LayXtreeVo;
import com.fast.admin.vo.ResourceTreeVo;
import com.fast.common.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：ylc
 * 时间：2018年5月21日17:18:44
 * 描述：树菜单工具类
 */
public class MenuTreeUtil {
    public final static List<ResourceTreeVo> buildTreeNode(List<ResourceTreeVo> trees) {
        List<ResourceTreeVo> menuTrees = new ArrayList<>();
        for (ResourceTreeVo tree : trees) {
            if (CommonUtil.properIsNULL(tree.getPid())){
                tree.setChildren(getChildrenNodes(tree.getId(), trees));
                menuTrees.add(tree);
            }
        }
        return menuTrees;
    }

    /**
     * 递归子获取子节点下的子节点
     * @param pId 父ID
     * @param treeList 所有菜单集合
     * @return
     */
    private final static List<ResourceTreeVo> getChildrenNodes(Long pId, List<ResourceTreeVo> treeList){
        List<ResourceTreeVo> newTree = new ArrayList<>();
        for (ResourceTreeVo tree : treeList) {
            if (CommonUtil.properIsNULL(pId)) continue;
            if (!CommonUtil.properIsNULL(tree.getPid()) && tree.getPid().equals(pId)){
                tree.setChildren(getChildrenNodes(tree.getId(), treeList));
                newTree.add(tree);
            }
        }
        return newTree;
    }


   /**======================================================================================================================*/
    public final static List<LayXtreeVo> buildLayXtreeNode(List<LayXtreeVo> trees) {
        List<LayXtreeVo> menuTrees = new ArrayList<>();
        for (LayXtreeVo tree : trees) {
            if (CommonUtil.properIsNULL(tree.getPid())){
                tree.setData(getChildrenNodesXtree(Long.valueOf(tree.getValue()), trees));
                menuTrees.add(tree);
            }
        }
        return menuTrees;
    }

    /**
     * 递归子获取子节点下的子节点
     * @param pId 父ID
     * @param treeList 所有菜单集合
     * @return
     */
    private final static List<LayXtreeVo> getChildrenNodesXtree(Long pId, List<LayXtreeVo> treeList){
        List<LayXtreeVo> newTree = new ArrayList<>();
        for (LayXtreeVo tree : treeList) {
            if (CommonUtil.properIsNULL(pId)) continue;
            if (!CommonUtil.properIsNULL(tree.getPid()) && tree.getPid().equals(pId)){
                tree.setData(getChildrenNodesXtree(Long.valueOf(tree.getValue()), treeList));
                newTree.add(tree);
            }
        }
        return newTree;
    }
}
